//
//  Settings.swift
//  Maze24
//
//  Created by Mars on 2021/1/15.
//

import SwiftUI

struct Settings: View {
  let team: [Member] = [
    Member(name: "Jiarui Wu", motto: "正道的光！！！", badge: "pencil.and.outline"),
    Member(name: "Rongxiao Liu", motto: "我要精进算法", badge: "book.circle"),
    Member(name: "Jiarui Li", motto: "下次我要赢到树莓派", badge: "hare"),
    Member(name: "Jinmo Wu", motto: "见识我的编程潜力吧", badge: "wand.and.stars"),
    Member(name: "Yannan Xu", motto: "战队新人，请多关照", badge: "flame")
  ]
  
  /// 1 - beginner
  /// 2 - intermediate
  /// 3 - advanced
  @AppStorage("level") var level: Int = 1
  @State var displaySheet: Bool = false
  @ObservedObject var model: Model
  
  var body: some View {
    ZStack {
      Color(UIColor.systemGray6).edgesIgnoringSafeArea(.all)
      
      VStack(alignment: .leading) {
        Text("关于我们")
          .fontWeight(.semibold)
          .font(.title2)
          .padding([.leading, .top], 15)
        
        buildNamecardScrollView(height: UIScreen.main.bounds.width * 0.4).padding(0)
        
        Form {
          Section(
            header: SectionHeader(iconName: "plusminus.circle.fill", title: "游戏难度").padding(.top, 25)) {
            Picker("游戏难度", selection: $level, content: {
              Text("简单").tag(1)
              Text("中级").tag(2)
              Text("困难").tag(3)
            })
            .onReceive([level].publisher.first()) { level in
              /// - TODO:
              /// Save level on change.
            }
          }
          
          Section(header: SectionHeader(iconName: "gamecontroller.fill", title: "游戏成就")) {
            NavigationLink(destination: Archievements(model: model)) {
              Text("我的成就")
            }
          }
          
          buildPrivacyPolicy()
        }
        .onAppear(perform: {
          UITableView.appearance().backgroundColor = .clear
        })
        
        Spacer()
      }
    }
    .ignoresSafeArea(edges: .bottom)
    .sheet(isPresented: self.$displaySheet, content: {
      WebView(url: "https://boxueio.com/copyright")
    })
    .navigationTitle(Text("Settings"))
  }
  
  func buildNamecardScrollView(height: CGFloat) -> some View {
    return ScrollView(.horizontal, showsIndicators: false, content: {
      HStack(spacing: 0) {
        ForEach(self.team, id: \.self) { member in
          Namecard(
            height: height,
            member: member)
            .padding(.horizontal)
        }
      }
    })
  }
  
  func buildPrivacyPolicy() -> some View {
    Section(header: SectionHeader(iconName: "doc.fill", title: "Documentary")) {
      Button(action: {
        self.displaySheet = true
      }, label: {
        Text("Privacy policy")
          .font(Font.system(.footnote))
      })
    }
  }
}

struct SectionHeader: View {
  let iconName: String
  let title: String
  
  var body: some View {
    return HStack {
      Image(systemName: iconName)
      Text(title)
    }
  }
}

struct Settings_Previews: PreviewProvider {
  static var previews: some View {
    Settings(model: Model())
  }
}
